package demo3;

import java.util.List;

public class MySingList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//永远指向头结点
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    //遍历单链表
    public void show(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+"  ");
            cur = cur.next;
        }
        System.out.println();
    }

    //单链表的长度
    public int size(){
        ListNode cur = head;
        int count  = 0;
        if(cur == null){
            return -1;
        }
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key){
        ListNode cur = head;
        if(cur == null){
            return false;
        }
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            return;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        //这里已经走到了尾巴节点
        cur.next = node;
    }
    public void addIndex(int index,int data){
        //任意位置插入一个数据
        int len = size();
        //判断index下表的合法性
        if(index < 0 || index > len){
            throw new IndexOutBounds("index下标不合法！");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == len){
            addLast(data);
            return;
        }
        //找到index-1的位置
        ListNode cur = findIndex(index);
         //进行插入
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;

    }
    private ListNode findIndex(int index){
        ListNode cur = head;
        while(index-1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }


    //删除对一次出现关键字key
    public void remove(int key){
        //找到删除的关键字的前驱结点
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
        }
       ListNode prev =  searchNode(key);
       if(prev == null){
           System.out.println("没有这个数据！！");
       }
       ListNode del = prev.next;
       prev.next = del.next;
    }

    private ListNode searchNode(int key){
        ListNode prev = head;
        while(prev.next != null){
            if(prev.next.val == key){
                return prev;
            }else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除链表中所有的key
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

    }

    //清空链表
    public void clear(){
        while(head != null){
            ListNode c = head.next;
            head.next = null;
            head = c;

        }
    }

    //链表的翻转
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;//要反转的结点
        head.next = null;
        while (cur != null){
            ListNode curNext = cur.next;//翻转结点的下一个结点
            cur.next = head;
            head = cur;
            cur = curNext;

        }
        return head;
    }
    public ListNode middleNode( ) {
        ListNode slow = head;
        ListNode fast  = head.next.next;
        if(head == null){
            return null;
        }
        while(fast != null && fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }

        return fast;
    }
    //找倒数第k个结点
    public ListNode lastNode(int k){
        if (k <= 0 || k > size()){
            return null;
        }
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //fast走k-1步
        for (int i = 0; i < k-1;k++){
            fast = fast.next;

        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode partition(int x){
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur =head;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                    //cur = cur.next;
                }else {
                    be.next = cur;
                    be = cur;
                    //cur = cur.next;
                }
                //cur = cur.next;
            } else {
                if(as == null){
                    as = cur;
                    ae = cur;
                    //cur = cur.next;
                }else {
                    ae.next = cur;
                    ae = cur;
                    //cur = cur.next;
                }
                //cur = cur.next;

                }
            cur = cur.next;
        }
        if(bs == null){
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        ListNode fast = head.next.next;
        ListNode slow = head;
        //找中间结点
        while(fast != null && fast.next != null){
            fast = fast.next;
            slow = slow.next;
    }
        //翻转链表
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //翻转完成
        //判断是否回文
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}
